Repository navigation
feat(query): add restore and restorePages to undo drop - #342
Merged
Merged
Conversation
A plain revert brings the whole list back and drops what was written since. They put removed items back into the data as it is by now, for the third argument of `$data`.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #342 +/- ##
==========================================
+ Coverage 83.92% 83.95% +0.02%
==========================================
Files 231 231
Lines 4958 4967 +9
Branches 1087 1090 +3
==========================================
+ Hits 4161 4170 +9
Misses 560 560
Partials 237 237 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
$data(key, value, undo)lets a write take back only its own change, but the function that undoes a removal from a list had to be written by hand in every app: find the item in the previous data, put it back where it stood, and leave the list alone if a refetch has already brought it back. A plain revert does not fit here: it brings the whole list back and drops what was written since, such as an item added while the removal was in flight.What
restore(array, prev, what)puts values removed withdropback at their previous indexes in the current array. It takes back every match, sincedropremoves them all, and leaves an array that holds a matching value again as it is, so nothing comes back twice.restorePages(pages, prev, what, callback)does the same for infinite pages, each page from the page with the same number inprev. The callback adapts the page shape, as insortPages. Pages stay as they are when a matching item is back on any page, and a page that is no longer loaded gets nothing back.$datapasses it toundo,nullincluded.sortorsortPageswhen the client knows the order of the list, an example next toundoin Reading and Writing Data, and a point in thenano-kit-queryskill, with itsDOCS.mdregenerated.Size
Only code that imports the helpers pays for them:
restoreadds 52 B on top ofdrop,restorePagesadds 125 B on top ofmapPagesanddrop.An item goes back to its previous index, so one added in the meantime can shift it by a place. Matching neighbours with an id selector closes that, but makes
restoretwo and a half times heavier, and a list with a known order gets the exact place fromsortandsortPagesanyway.Tests
utils.spec.ts: one value and several values put back, what was written since, an array that became shorter or empty, no mutation of the input, a value that is back already, empty values, and for pages the page an item stood in, the pagination of the current pages, items of several pages, a custom page shape, an item back on another page, a page loaded since and a page no longer loaded.cache.spec.ts: both helpers as theundoof a real$datawrite.oxlint,tscandvitestare clean in@nano_kit/query(214 tests),size-limitpasses, the pre-pushpnpm testpasses across the monorepo and the website builds.